class Solution:
    def oneEditAway(self, first: str, second: str) -> bool:
        if len(first) < len(second):
            return self.oneEditAway(second, first)
        m, n = len(first), len(second)
        if m - n > 1:
          return False
        if m == n:
            countDiff = 0
            for i in range(m):
                if first[i] != second[i]:
                    countDiff += 1
                    if countDiff > 1:
                        return False
            return True
        else:
            offsetI, i = 0, 0
            
            while i < n:
                charA, charB = first[i + offsetI], second[i]
                if charA != charB:
                    offsetI += 1
                    i -= 1
                if offsetI > 1:
                    return False
                i += 1
        return True


    def oneEditAway2(self, first: str, second: str) -> bool:
        m, n = len(first), len(second)
        if m < n:
            return self.oneEditAway(second, first)
        if m - n > 1:
            return False
        for i, (x, y) in enumerate(zip(first, second)):
            if x != y:
                return first[i + 1:] == second[i + 1:] if m == n else first[i + 1:] == second[i:]  # 注：改用下标枚举可达到 O(1) 空间复杂度
        return True
